******SET YOUR DIRECTORY to ...\replication




///////////////////////////////////////////////////////////////////////////////////////////
///Table A.7
///////////////////////////////////////////////////////////////////////////////////////////
use "finaldata\final_cc_data.dta", clear
drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991

*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
g bad_soil = 1- good_soil

***Regressions
*Census division FE
reghdfe bad_soil if year==1991, absorb(cdivcode)
outreg2 vc_pc using table_bs_regvar, keep() tex(frag) nocon noni nonotes label ctitle("High LB") addtext(C. div FE, YES, C. reg FE, NO, State FE, NO, Estimation, OLS) replace
*Census region FE
reghdfe bad_soil if year==1991, absorb(cregcode)
outreg2 vc_pc using table_bs_regvar, keep() tex(frag) nocon noni nonotes label ctitle("High LB") addtext(C. div FE, NO, C. reg FE, YES, State FE, NO, Estimation, OLS)
*State FE
reghdfe bad_soil if year==1991, absorb(STATEFP00)
outreg2 vc_pc using table_bs_regvar, keep() tex(frag) nocon noni nonotes label ctitle("High LB") addtext(C. div FE, NO, C. reg FE, NO, State FE, YES, Estimation, OLS)





//////////////////////////
///TABLE A.8
/////////////////////////
use "finaldata\final_cc_data.dta", clear
drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991

*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
gen inter_goodtetra=good_soil*tetra_corr
gen inter_badtetra=(1-good_soil)*tetra_corr
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)
gen vc_pc=totnpcc_cc_offenses_vc*1000


*Label variables
label var vc_pc "Violent crime Rate"
label var inter_badtetra "High LB x Lead"

drop if  cdivcode==.

***All years
*Year+MSA+Region FE
reghdfe vc_pc inter_badtetra, absorb(fipsplace_00 year) vce(conventional)
test _b[inter_badtetra] = 0
local ff = trim("`: display %10.2f r(F)'")
outreg2 inter_badtetra using table_rob_fs_reg, replace tex(frag) nocon noni nonotes nor2 label ctitle("Violent crime") addtext(MSA FE, YES, Year FE, YES, C. region X Year FE, NO, C. division X Year FE, NO, Year, 60-91, Estimation, OLS, F, `ff')
*Year+MSA+Region FE
reghdfe vc_pc inter_badtetra, absorb(fipsplace_00 year cregcode#year) vce(conventional)
test _b[inter_badtetra] = 0
local ff = trim("`: display %10.2f r(F)'")
outreg2 inter_badtetra using table_rob_fs_reg, tex(frag) nocon noni nonotes nor2 label ctitle("Violent crime") addtext(MSA FE, YES, Year FE, YES, C. region X Year FE, YES, C. division X Year FE, NO, Year, 60-91, Estimation, OLS, F, `ff')
*Year+MSA+Division FE
reghdfe vc_pc inter_badtetra, absorb(fipsplace_00 year cdivcode#year) vce(conventional)
test _b[inter_badtetra] = 0
local ff = trim("`: display %10.2f r(F)'")
outreg2 inter_badtetra using table_rob_fs_reg, tex(frag) nocon noni nonotes nor2 label ctitle("Violent crime") addtext(MSA FE, YES, Year FE, YES, C. region X Year FE, NO, C. division X Year FE, YES, Year, 60-91, Estimation, OLS, F, `ff')

***Census years
*Year+MSA+Region FE
reghdfe vc_pc inter_badtetra if year==1960 | year==1970 | year==1980 | year==1990, absorb(fipsplace_00 year) vce(conventional)
test _b[inter_badtetra] = 0
local ff = trim("`: display %10.2f r(F)'")
outreg2 inter_badtetra using table_rob_fs_reg, tex(frag) nocon noni nonotes nor2 label ctitle("Violent crime") addtext(MSA FE, YES, Year FE, YES, C. region X Year FE, NO, C. division X Year FE, NO, Year, CY60-90, Estimation, OLS, F, `ff')
*Year+MSA+Region FE
reghdfe vc_pc inter_badtetra if year==1960 | year==1970 | year==1980 | year==1990, absorb(fipsplace_00 year cregcode#year) vce(conventional)
test _b[inter_badtetra] = 0
local ff = trim("`: display %10.2f r(F)'")
outreg2 inter_badtetra using table_rob_fs_reg, tex(frag) nocon noni nonotes nor2 label ctitle("Violent crime") addtext(MSA FE, YES, Year FE, YES, C. region X Year FE, YES, C. division X Year FE, NO, Year, CY60-90, Estimation, OLS, F, `ff')
*Year+MSA+Division FE
reghdfe vc_pc inter_badtetra if year==1960 | year==1970 | year==1980 | year==1990, absorb(fipsplace_00 year cdivcode#year) vce(conventional)
test _b[inter_badtetra] = 0
local ff = trim("`: display %10.2f r(F)'")
outreg2 inter_badtetra using table_rob_fs_reg, tex(frag) nocon noni nonotes nor2 label ctitle("Violent crime") addtext(MSA FE, YES, Year FE, YES, C. region X Year FE, NO, C. division X Year FE, YES, Year, CY60-90, Estimation, OLS, F, `ff')




///////////////////////////////////////////////////////////////////////////////////////////
///TABLE A.9
///////////////////////////////////////////////////////////////////////////////////////////
use "finaldata\final_cc_data.dta", clear
drop if fipsplace_00==.
xtset fipsplace_00 year
label var tetra_corr "Tetraethyl Lead"
label var ph1_plc_wtm_wtm_0_r "pH"

drop if year>1991

gen vc_pc=totnpcc_cc_offenses_vc*1000

///Regression with bounds
label var inter_tlXph_0_50_55 "pH between 5 and 5.5 x Lead"
label var inter_tlXph_0_55_60 "pH between 5.5 and 6 x Lead"
label var inter_tlXph_0_60_65 "pH between 6 and 6.5 x Lead"
label var inter_tlXph_0_65_70 "pH between 6.5 and 7 x Lead"
label var inter_tlXph_0_70_75 "pH between 7 and 7.5 x Lead"
label var inter_tlXph_0_75_80 "pH between 7.5 and 8 x Lead"
label var inter_tlXph_0_80_85 "pH between 8 and 8.5 x Lead"
xtreg vc_pc i.year inter_tlXph_0_5* inter_tlXph_0_6* inter_tlXph_0_7* inter_tlXph_0_8*, fe vce(robust)
outreg2 inter_tlXph_0_5* inter_tlXph_0_6* inter_tlXph_0_7* inter_tlXph_0_8* using table_first_bounds, tex(frag) label nocon noni nonotes ctitle("Violent crime") keep(inter_tlXph_0_5* inter_tlXph_0_6* inter_tlXph_0_7* inter_tlXph_0_8*) addtext(MSA FE, YES, Year FE, YES, Estimation, OLS) replace




///////////////////////////////////////////////////////////////////////////////////////////
///FIGURE A.13//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////// 
set matsize 2000

grstyle init
grstyle set plain
grstyle set color s2


use "finaldata\final_cc_data.dta", clear
drop if fipsplace_00==.
xtset fipsplace_00 year
label var tetra_corr "Tetraethyl Lead"
label var ph1_plc_wtm_wtm_0_r "pH"

drop if year>1991


gen vc_pc=totnpcc_cc_offenses_vc * 1000

///Margins
reghdfe vc_pc tetra_corr c.tetra_corr#c.ph1_plc_wtm_wtm_0_r c.tetra_corr#c.ph1_plc_wtm_wtm_0_r#c.ph1_plc_wtm_wtm_0_r c.tetra_corr#c.ph1_plc_wtm_wtm_0_r#c.ph1_plc_wtm_wtm_0_r#c.ph1_plc_wtm_wtm_0_r , abs(fipsplace_00 cdivcode) vce(cluster cdivcode#year)
margins , at(ph1_plc_wtm_wtm_0_r=(4.8(0.2)8)) dydx(tetra_corr)
marginsplot, title("Average marginal effects of past lead with 95% CIs")
graph export figure_margin_p3.png, as(png) replace





///////////////////////////////////////////////////////////////////////////////////////////
///Table A.10
///////////////////////////////////////////////////////////////////////////////////////////

use "finaldata\final_cc_data.dta", clear

drop if fipsplace_00==.
xtset fipsplace_00 year

*drop if year>1991

*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
g bad_soil = 1 - good_soil
gen inter_goodtetra=good_soil*tetra_corr
gen inter_badtetra=(1-good_soil)*tetra_corr
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)
g pop_msa = tot_ncc_oripop_corr + tot_cc_oripop_corr

*Create per capita variables
gen vc_pc=totnpcc_cc_offenses_vc*1000

*Cluster variables
gen cdivcodeyear=cdivcode*year
gen cregcodeyear=cregcode*year

*Label variables
label var vc_pc "Violent crime"
label var inter_badtetra "High LB x Lead"
label var bad_soil "High LB"


merge 1:1 FMSA year using "finaldata\lead_blood.dta" 

drop if _merge==1

rename good_soil _temp
bysort FMSA: egen good_soil = min(_temp)
drop _temp

rename cregcode _temp
bysort FMSA: egen cregcode = min(_temp)
drop _temp

rename cdivcode _temp
bysort FMSA: egen cdivcode = min(_temp)
drop _temp


reg perc_positive5 bad_soil, cluster(FMSA)
margins, at(bad_soil = (0 1))
outreg2 using table_leadblood, tex(frag) nocon noni nor2 nonotes keep(bad_soil) label ctitle("% BLL>5") addtext(Year measurement FE, NO, Correction missing, NO, Estimation, OLS)  replace

reg perc_positive10 bad_soil, cluster(FMSA)
margins, at(bad_soil = (0 1))
outreg2 using table_leadblood, tex(frag) nocon noni nor2 nonotes keep(bad_soil) label ctitle("% BLL>10") addtext(Year measurement FE, NO, Correction missing, NO, Estimation, OLS) 


reg perc_positive5 bad_soil i.year, cluster(FMSA)
margins, at(bad_soil = (0 1))
outreg2 using table_leadblood, tex(frag) nocon noni nor2 nonotes keep(bad_soil) label ctitle("% BLL>5") addtext(Year measurement FE, YES, Correction missing, NO, Estimation, OLS)

reg perc_positive10 bad_soil i.year, cluster(FMSA)
margins, at(bad_soil = (0 1))
outreg2 using table_leadblood, tex(frag) nocon noni nor2 nonotes keep(bad_soil) label ctitle("% BLL>10") addtext(Year measurement FE, YES, Correction missing, NO, Estimation, OLS) 

replace perc_positive5 = 0 if perc_positive5==. & num_tested!=.
replace perc_positive10 = 0 if perc_positive10==. & num_tested!=.

reg perc_positive5 bad_soil i.year, cluster(FMSA)
margins, at(bad_soil = (0 1))
outreg2 using table_leadblood, tex(frag) nocon noni nor2 nonotes keep(bad_soil) label ctitle("% BLL>5") addtext(Year measurement FE, YES, Correction missing, YES, Estimation, OLS) 

reg perc_positive10 bad_soil i.year, cluster(FMSA)
margins, at(bad_soil = (0 1))
outreg2 using table_leadblood, tex(frag) nocon noni nor2 nonotes keep(bad_soil) label ctitle("% BLL>10") addtext(Year measurement FE, YES, Correction missing, YES, Estimation, OLS) 


///////////////////////////////////////////////////////////////////////////////////////////
///FIGURE A.14///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
use "finaldata\final_cc_data.dta", clear
drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991

*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
gen inter_goodtetra=good_soil*tetra_corr
gen inter_badtetra=(1-good_soil)*tetra_corr
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)

*Create standardized variables
egen sd_vc=sd(totnpcc_cc_offenses_vc)
egen sd_pop_cc=sd(tot_cc_oripop_corr)
egen sd_pop_ncc=sd(tot_ncc_oripop_corr)
gen vc_pc=totnpcc_cc_offenses_vc * 1000
gen standardized_pop_cc=tot_cc_oripop_corr/sd_pop_cc
gen standardized_pop_ncc=tot_ncc_oripop_corr/sd_pop_ncc
g lncc = ln(tot_cc_oripop_corr)
g lnncc = ln(tot_ncc_oripop_corr)

*Label variables
label var vc_pc "Violent crime"
label var inter_goodtetra "Low LB x Lead"

***Locals

*Suburbanization
egen pop_cc_us=total(tot_cc_oripop_corr),by(year)
egen pop_ncc_us=total(tot_ncc_oripop_corr),by(year)
gen perc_cc_us=pop_cc_us/(pop_cc_us+pop_ncc_us)


***First stage
*
egen id = group(fipsplace_00)
local n_iter = 1000
set matsize  `n_iter'
mat betas = J(`n_iter',1,1)
qui {
g FAKE_treat = .
forval iter = 1/`n_iter' {
	noisily di "*************"
	noisily di "NUM ITERATION ENDED"
	noisily di `iter'
	noisily di "************"
	g FAKE_bad_soil = 0
	forval city = 1/325 {

		local draw = runiform()
		if `draw'> .1409 {
			replace FAKE_bad_soil = 1 if id ==`city'
		}	
	}
	replace FAKE_treat=FAKE_bad_soil*tetra_corr
	reghdfe vc_pc FAKE_treat, abs(fipsplace_00 year cdivcode#year) 
	mat betas[`iter',1] = _b[FAKE_treat]
	drop FAKE_bad_soil

}
} 

g plbetas = betas[_n,1] in 1/1000
reghdfe vc_pc inter_badtetra, abs(fipsplace_00 year cdivcode#year) 

local effect = _b[inter_badtetra]
hist plbetas, xline(`effect',lw(1.2)) xtitle({&chi}, size(large)) w(0.25)   frac



///////////////////////////////////////////////////////////////////////////////////////////
///FIGURE A.15///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
use "finaldata\final_cc_data.dta", clear
drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991

*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
g bad_soil = 1-good_soil
gen inter_goodtetra=good_soil*tetra_corr
gen inter_badtetra=(1-good_soil)*tetra_corr
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)

*Generate other variables
gen pop_msa=tot_cc_oripop_corr+tot_ncc_oripop_corr
gen pop_msa_1960_2=pop_msa if year==1960
egen pop_msa_1960=max(pop_msa_1960_2) ,by(FMSA)
gen vc_pc=totnpcc_cc_offenses_vc*1000
egen cregcodeyear=group(cregcode year)
egen cdivcodeyear=group(cdivcode year)

drop if year==1950

egen id = group(year)
forvalues i=1/32{
	gen tetra_corr_`i'_2=tetra_corr if id==`i'
	egen tetra_corr_`i'=max(tetra_corr_`i'_2)
	drop tetra_corr_`i'_2
}

local n_iter = 1000
set matsize  `n_iter'
mat betas = J(`n_iter',1,1)
qui {
g FAKE_treat = .
forval iter = 1/`n_iter' {
	noisily di "*************"
	noisily di "NUM ITERATION ENDED"
	noisily di `iter'
	noisily di "************"
	g FAKE_tetra_corr = 0
	
	gen draw = int(runiform(1,32)) if FMSA==40

	forval yy = 1/32 {
		sum draw if id == `yy'
		local FAKE_year=`r(mean)'
		sum tetra_corr_`FAKE_year'
		replace FAKE_tetra_corr = `r(mean)' if id ==`yy'
	
	}
	replace FAKE_treat=bad_soil*FAKE_tetra_corr
	reghdfe vc_pc FAKE_treat, abs(fipsplace_00 year cdivcode#year) 
	mat betas[`iter',1] = _b[FAKE_treat]
	drop FAKE_tetra_corr draw

}
} 

g plbetas = betas[_n,1] in 1/`n_iter'
reghdfe vc_pc inter_badtetra, abs(fipsplace_00 year cdivcode#year) 

local effect = _b[inter_badtetra]
hist plbetas, xline(`effect',lw(1.2)) xtitle({&chi}, size(large)) w(0.25) xlabel(-3(1)3.5) frac
graph export placebo_yearstetra.png, as(png) replace
